package com.patrol.logisticsmanage.ui.orders;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.patrol.logisticsmanage.R;

import java.util.List;
import java.util.Map;

public class OrdersAdapter extends RecyclerView.Adapter<OrdersAdapter.ViewHolder> {

    // 普通的item ViewType
    private static final int TYPE_ITEM = 1;
    // 空布局的ViewType
    private static final int TYPE_EMPTY = 2;

    private Context mContext;

    // 数据
    private List<OrderInfo> mList;

    // 是否显示空布局，默认不显示
    private boolean showEmptyView = false;

    public OrdersAdapter(Context context) {
        mContext = context;
    }

//    FoodAdapter foodAdapter;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_EMPTY) {
            // 创建空布局item
            return new ViewHolder(getEmptyView(parent));
        } else {
            // 创建普通的item
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_orders, parent, false);
            return new ViewHolder(view);
        }
    }

    /**
     * 获取空布局
     */
    private View getEmptyView(ViewGroup parent) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.empty_page, parent, false);
        return view;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 如果是空布局item，不需要绑定数据
        if (!isEmptyPosition(position)) {
            OrderInfo o = mList.get(position);
            ((TextView)holder.cardView.findViewById(R.id.ddtime)).setText(o.getDt());
            ((TextView)holder.cardView.findViewById(R.id.ddamount)).setText("¥" + o.getAmount());
            ((TextView)holder.cardView.findViewById(R.id.taihao)).setText(o.getDevice_id());
            holder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ARouter.getInstance().build("/app/orderDetails").withString("outid",o.getOutid()).navigation();
                }
            });
//            holder.cardView.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
//            holder.cardView.set
        }
    }

    @Override
    public int getItemCount() {
        // 判断数据是否空，如果没有数据，并且需要显示空布局，就返回1。
        int count = mList != null ? mList.size() : 0;
        if (count > 0) {
            return count;
        } else if (showEmptyView) {
            // 显示空布局
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isEmptyPosition(position)) {
            // 空布局
            return TYPE_EMPTY;
        } else {
            return TYPE_ITEM;
        }
    }

    public void setList(List<OrderInfo> list) {
        mList = list;
        notifyDataSetChanged();
    }

    /**
     * 判断是否是空布局
     */
    public boolean isEmptyPosition(int position) {
        int count = mList != null ? mList.size() : 0;
        return position == 0 && showEmptyView && count == 0;
    }

    /**
     * 设置空布局显示。默认不显示
     */
    public void showEmptyView(boolean isShow) {
        if (isShow != showEmptyView) {
            showEmptyView = isShow;
            notifyDataSetChanged();
        }
    }

    public boolean isShowEmptyView() {
        return showEmptyView;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        CardView cardView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.rcy_item_content);
        }
    }

}